/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_GB_MEDIA_SERVER_H
#define MK_GB_MEDIA_SERVER_H
#include "MkGb28181/MkGb28181.h"
#include "MkMedia/MkMediaSource.h"
#include "MkUtil/MkLock.h"
#include "MkUtil/MkRwLock.h"

class CMkPsStreamSource;
class CMkRtpPacket;
class CMkRtcpPacket;
class CMkRtpServerPool;
class CMkRtpTcpServer;
class CMkRtpUdpServer;
class CMkGbStreamSource;
class CMkRtp;
class CMkGbConsumer;
typedef MkFunction<void()> MkGbRegServerStateChangeCallback;
class MkApi CMkGbMediaServer : public CMkGb28181
{
    typedef struct _gb_invite_param
    {
        CMkRtpTcpServer*    pTcpServer;
        CMkRtpUdpServer*    pUdpServer;
        MkString            StreamPath;
        CMkRtp*             pUseRtp;
        MkString            RemoteHost;
        Uint16              RemotePort;
        Uint8               Payload;
        Uint32              Ssrc;
        BOOL                bConsumer;   //if not consumer  then is publisher
        BOOL                bNeedAckSdp;
        _gb_invite_param()
        {
            pTcpServer = nullptr;
            pUdpServer = nullptr;
            pUseRtp = nullptr;
            bConsumer = FALSE;
            bNeedAckSdp = FALSE;
        }
    }MkGbInviteParam;
public:
    CMkGbMediaServer(CMkEventPool* pEventPool, CMkTimer* pTimer, CMkRtpServerPool* pRtpPool, const MkGbRegServerStateChangeCallback& RegStateChangeCb, const MkMediaCallbacks& MediaCallbacks);
    ~CMkGbMediaServer();

    Uint32 Start(const MkGbStartParam& Param, const MkString& AppName);
    Uint32 SetRegisterServer(const MkString& ServerId, const MkString& ServerRealm, const MkString& ServerHost, Uint16 Port, BOOL bTcpSip, const MkString& UserName, const MkString& Password, Uint32 Expires, Uint32 HbInterval, Uint32 HbCount);
    BOOL IsGbRegisterServerOnline() { return m_RegSuccess; }
protected:
    Uint32 OnGbRequest(const CMkGbRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbMessageRequest(const CMkGbMessageRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbInviteReqeust(const CMkGbInviteReqeust* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbAckRequest(const CMkGbAckReqeust* pGbReq);
    Uint32 OnGbByeRequest(const CMkGbByeReqeust* pGbReq, const MkGbResponseCallback& Cb);
private:
    Uint32 RegisterServer(const CMkGbDeviceAuthorRequest& Req, BOOL bFailEnd = FALSE);
    Uint32 KeepAliveServer();
private:
    Uint32 OnRtpPacket(CMkGbStreamSource* pMediaSource, const CMkRtpPacket& RtpPacket, const MkString& Host, Uint16 Port);
    Uint32 OnRtcpPacket(CMkGbStreamSource* pMediaSource, const CMkRtcpPacket& RtcpPacket, const MkString& Host, Uint16 Port);
private:
    CMkRtpServerPool*                       m_pRtpPool;
    Uint32                                  m_RtcpTimeOutSec;
    MkString                                m_TimerCheckSession;
    MkMediaCallbacks                        m_MediaCbs;
    MkGbRegServerStateChangeCallback        m_RegStateChangeCb;

    CMkLock                                 m_InviteLock;
    MkMap<MkString, MkGbInviteParam>        m_MapInvite;   //key invite session
    CMkLock                                 m_GbConsumerLock;
    MkMap<MkString, CMkGbConsumer*>         m_MapGbConsumer;

    MkString                                m_RegServerId;
    MkString                                m_RegServerRealm;
    MkString                                m_RegHost;
    Uint16                                  m_RegPort;
    BOOL                                    m_RegTcpSip;
    MkString                                m_RegServerUserName;
    MkString                                m_RegServerPassword;
    Uint32                                  m_RegExpires;
    Uint32                                  m_HbInterval;
    Uint32                                  m_HbCount;
    BOOL                                    m_RegSuccess;
    CMkTime                                 m_LastActiveTime;
    MkString                                m_KeepAliverTimer;
    MkString                                m_RegisterDelayer;
    MkString                                m_AppName;
};


#endif